package com.school.empsys.service.impl;

import com.school.empsys.constants.UserStatus;
import com.school.empsys.mapper.EmpContractMapper;
import com.school.empsys.mapper.UserMapper;
import com.school.empsys.model.EmpContract;
import com.school.empsys.model.User;
import com.school.empsys.service.EmpContractService;
import com.school.empsys.utils.DateUtils;
import com.school.empsys.utils.FileUtil;
import com.school.empsys.utils.StrUtil;
import com.school.empsys.utils.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class EmpContractServiceImpl implements EmpContractService {

    private static Logger logger = LoggerFactory.getLogger(EmpContractServiceImpl.class);

    @Autowired
    EmpContractMapper empContractMapper;

    @Autowired
    UserMapper userMapper;

    @Value("${files.path}")
    private String filesPath;

    @Override
    public int count(Map<String, Object> params) {
        return empContractMapper.count(params);
    }

    @Override
    public List<EmpContract> list(Map<String, Object> params, Integer offset, Integer limit) {
        return empContractMapper.list(params, offset, limit);
    }

    @Override
    public void save(EmpContract empContract) {
        Assert.notNull(empContract.getUserId(), "合同所属用户不能为空");
        Assert.notNull(empContract.getUrl(), "图片不能为空");
        String fullPathSource = filesPath + empContract.getUrl();
        File sourceFile = new File(fullPathSource);
        String nowString = DateUtils.nowString();

        String targetPath = FileUtil.getPath() + nowString + "/" + sourceFile.getName();
        FileUtil.saveFile(sourceFile, filesPath + targetPath);

        empContract.setContractNo(nowString);
        empContract.setCreateTime(new Date());
        empContract.setUpdateTime(new Date());
        empContract.setUrl(targetPath);
        empContract.setStatus(1);

        empContractMapper.insertSelective(empContract);
    }

    @Override
    public EmpContract findOne(Integer id) {
        EmpContract empContract = empContractMapper.selectByPrimaryKey(id);
        User user = userMapper.selectByPrimaryKey(empContract.getUserId());
        String img2Base64 = FileUtil.img2Base64(filesPath + empContract.getUrl());
        empContract.setUrl(img2Base64);
        empContract.setUser(user);
        return empContract;
    }

    /**
     * 员工合同到期自动作废，员工账号改为锁定
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void expirtEmpContract() {
        EmpContract param = new EmpContract();
        param.setStatus(1);
        param.setExpireDate(new Date());
        List<EmpContract> empContracts = empContractMapper.selectByEmpContract(param);
        if (!CollectionUtils.isEmpty(empContracts)) {
            for (EmpContract empContract : empContracts) {
                empContract.setStatus(0);
                empContract.setUpdateTime(new Date());
                empContractMapper.updateByPrimaryKeySelective(empContract);

                User user = empContract.getUser();
                user.setStatus(UserStatus.LOCKED.getCode());
                user.setUpdateTime(new Date());
                userMapper.updateByPrimaryKeySelective(user);
            }
        }
    }
}
